<template>
  <div class="app-container">
    <el-form
      v-show="showSearch"
      :model="queryParams"
      ref="queryForm"
      size="small"
      inline
      label-width="68px"
    >
      <el-form-item label="名称" prop="name">
        <el-input
          v-model="queryParams.name"
          placeholder="请输入名称"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">
          搜索
        </el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          v-hasPermi="['tianqi:solution:add']"
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
        >
          新增
        </el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          v-hasPermi="['tianqi:solution:remove']"
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
        >
          批量删除
        </el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList" />
    </el-row>

    <el-table
      v-loading="loading"
      :data="dataList"
      @selection-change="handleSelectionChange"
    >
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="序号" type="index" width="50" align="center" />
      <el-table-column label="方案全称" align="center" prop="name" />
      <el-table-column label="方案简称" align="center" prop="shortName" />
      <el-table-column label="英文名称" align="center" prop="englishName" />
      <el-table-column label="图标" align="center" prop="image" width="100">
        <template slot-scope="scope">
          <image-preview :src="scope.row.image" :width="50" :height="50" />
        </template>
      </el-table-column>
      <el-table-column label="大图" align="center" prop="image2" width="100">
        <template slot-scope="scope">
          <image-preview :src="scope.row.image2" :width="50" :height="50" />
        </template>
      </el-table-column>
      <el-table-column label="展示图" align="center" prop="image3" width="100">
        <template slot-scope="scope">
          <image-preview :src="scope.row.image3" :width="50" :height="50" />
        </template>
      </el-table-column>
      <el-table-column label="架构部署图" align="center" prop="image4" width="100">
        <template slot-scope="scope">
          <image-preview :src="scope.row.image4" :width="50" :height="50" />
        </template>
      </el-table-column>
      <el-table-column label="系统架构图" align="center" prop="image5" width="100">
        <template slot-scope="scope">
          <image-preview :src="scope.row.image5" :width="50" :height="50" />
        </template>
      </el-table-column>
      <el-table-column label="顶部菜单" align="center" width="90">
        <template slot-scope="scope">
          <el-switch
            v-model="scope.row.top"
            :active-value="1"
            :inactive-value="0"
            :disabled="scope.row.status == 1"
            @change="handletopChange(scope.row)"
          />
        </template>
      </el-table-column>
      <el-table-column label="是否推荐" align="center" width="90">
        <template slot-scope="scope">
          <el-switch
            v-model="scope.row.recommend"
            :active-value="1"
            :inactive-value="0"
            :disabled="scope.row.status == 1"
            @change="handleRecommendChange(scope.row)"
          />
        </template>
      </el-table-column>
      <el-table-column label="首页展示" align="center" width="90">
        <template slot-scope="scope">
          <el-switch
            v-model="scope.row.displayHome"
            :active-value="1"
            :inactive-value="0"
            :disabled="scope.row.status == 1"
            @change="handleDisplayHomeChange(scope.row)"
          />
        </template>
      </el-table-column>
      <el-table-column label="是否展示" align="center" width="100">
        <template slot-scope="scope">
          <el-switch
            v-model="scope.row.status"
            :active-value="0"
            :inactive-value="1"
            @change="handleStatusChange(scope.row)"
          />
        </template>
      </el-table-column>
      <el-table-column label="排序" align="center" prop="sort" width="80" />
      <el-table-column label="操作" align="center">
        <template slot-scope="scope">
          <el-button
            v-hasPermi="['tianqi:solution:edit']"
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
          >
            修改
          </el-button>
          <el-button
            v-hasPermi="['tianqi:solution:remove']"
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
          >
            删除
          </el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total > 0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改解决方案对话框 -->
    <el-dialog
      :title="editForm.id ? '修改解决方案' : '新增解决方案'"
      :visible.sync="open"
      width="860px"
      append-to-body
      :close-on-click-modal="false"
    >
      <el-form ref="editForm" :model="editForm" :rules="rules" label-width="98px">
        <el-row>
          <el-col :span="12">
            <el-form-item label="方案全称" prop="name">
              <el-input
                v-model="editForm.name"
                placeholder="请输入方案全称"
                maxlength="30"
              />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="方案简称" prop="shortName">
              <el-input
                v-model="editForm.shortName"
                placeholder="请输入方案简称"
                maxlength="30"
              />
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="12">
            <el-form-item label="英文名" prop="englishName">
              <el-input
                v-model="editForm.englishName"
                placeholder="请输入英文名"
                maxlength="100"
              />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="排序" prop="sort">
              <el-input v-model="editForm.sort" placeholder="请输入排序" type="number" />
            </el-form-item>
          </el-col>
        </el-row>
        <el-form-item label="简介" prop="synopsis">
          <el-input
            v-model="editForm.synopsis"
            placeholder="请输入简介"
            maxlength="200"
          />
        </el-form-item>
        <el-form-item label="介绍" prop="introduce">
          <el-input
            v-model="editForm.introduce"
            type="textarea"
            placeholder="请输入内容"
            maxlength="500"
          />
        </el-form-item>
        <el-row>
          <el-col :span="8">
            <el-form-item label="图标" prop="image">
              <image-upload v-model="editForm.image" :limit="1" :is-show-tip="false" />
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="大图" prop="image2">
              <image-upload v-model="editForm.image2" :limit="1" :is-show-tip="false" />
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="展示图" prop="image3">
              <image-upload v-model="editForm.image3" :limit="1" :is-show-tip="false" />
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="8">
            <el-form-item label="架构部署图" prop="image4">
              <image-upload v-model="editForm.image4" :limit="1" :is-show-tip="false" />
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="系统架构图" prop="image5">
              <image-upload v-model="editForm.image5" :limit="1" :is-show-tip="false" />
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import {
  listSolution,
  getSolution,
  delSolution,
  addSolution,
  updateSolution,
  updateSolutionDisplayHome,
} from "@/api/tianqi/solution";

export default {
  name: "Solution",
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 解决方案表格数据
      dataList: [],
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        name: null,
      },
      // 表单参数
      editForm: {},
      // 表单校验
      rules: {
        name: [{ required: true, message: "方案全称不能为空", trigger: "blur" }],
        shortName: [{ required: true, message: "方案简称不能为空", trigger: "blur" }],
        image: [{ required: true, message: "请上传图标", trigger: "blur" }],
        image2: [{ required: true, message: "请上传大图", trigger: "blur" }],
        image3: [{ required: true, message: "请上传展示图", trigger: "blur" }],
      },
    };
  },
  created() {
    this.getList();
  },
  methods: {
    /** 查询解决方案列表 */
    getList() {
      this.loading = true;
      listSolution(this.queryParams)
        .then((res) => {
          this.dataList = res.rows;
          this.total = res.total;
        })
        .finally(() => {
          this.loading = false;
        });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.editForm = {
        id: null,
        sort: null,
        status: null,
        name: null,
        shortName: null,
        englishName: null,
        synopsis: null,
        introduce: null,
        top: null,
        recommend: null,
        displayHome: null,
        image: null,
        image2: null,
        image3: null,
        image4: null,
        image5: null,
      };
      this.resetForm("editForm");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map((item) => item.id);
      this.single = selection.length !== 1;
      this.multiple = !selection.length;
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
    },
    handletopChange(row) {
      const next = Number(row.top);
      const prev = next === 1 ? 0 : 1;
      const text = next === 1 ? "置顶" : "取消置顶";
      this.$modal
        .confirm("确认要" + text + "该方案吗？")
        .then(() => {
          updateSolution({ id: row.id, top: next }).then(() => {
            this.getList();
          });
        })
        .catch(() => {
          row.top = prev;
        });
    },
    handleRecommendChange(row) {
      const next = Number(row.recommend);
      const prev = next === 1 ? 0 : 1;
      const text = next === 1 ? "推荐" : "取消推荐";
      this.$modal
        .confirm("确认要" + text + "该方案吗？")
        .then(() => {
          updateSolution({ id: row.id, recommend: next }).then(() => {
            this.getList();
          });
        })
        .catch(() => {
          row.recommend = prev;
        });
    },
    handleStatusChange(row) {
      const next = Number(row.status);
      const prev = next === 0 ? 1 : 0;
      const text = next === 0 ? "展示" : "隐藏";
      this.$modal
        .confirm("确认要" + text + "该内容吗？")
        .then(() => {
          updateSolution({ id: row.id, status: next }).then(() => {
            this.getList();
          });
        })
        .catch(() => {
          row.status = prev;
        });
    },
    // 设置首页展示
    handleDisplayHomeChange(row) {
      const next = Number(row.displayHome);
      const prev = next === 1 ? 0 : 1;
      const text = next === 1
        ? "开启后，该条数据将在首页展示，其余将自动取消首页展示"
        : "取消后，首页将不展示该模块";
      this.$modal
        .confirm(text)
        .then(() => {
          updateSolutionDisplayHome({ id: row.id, displayHome: next }).then(() => {
            this.getList();
          });
        })
        .catch(() => {
          row.displayHome = prev;
        });
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      getSolution(row.id).then((res) => {
        this.editForm = res.data;
        this.open = true;
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["editForm"].validate((valid) => {
        if (valid) {
          let request = this.editForm.id ? updateSolution : addSolution;
          request(this.editForm).then((res) => {
            this.$modal.msgSuccess("操作成功");
            this.open = false;
            this.getList();
          });
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$modal
        .confirm("是否确认删除该解决方案？")
        .then(() => {
          delSolution(ids).then(() => {
            this.getList();
            this.$modal.msgSuccess("删除成功");
          });
        })
        .catch(() => {});
    },
  },
};
</script>
